package b1.controls.basic
{
	import b1.net.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.ui.Mouse;
	
	public class SoundIcon extends Sprite
	{
		private var tip:BasicMessage;
		private static var soundFlag:Boolean = true; 
		
		public function SoundIcon()
		{
			super();
			//图标
			var Loudspeaker:Class = ResourceHelper.getInstance().getClassByName("Loudspeaker");
			var iconImage:Sprite = new Loudspeaker();
			iconImage.width = 30;
			iconImage.height = 30;
			addChild(iconImage);
			refresh();
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		public function refresh():void
		{
			if (soundFlag)
				enableSound();
			else
				disableSound();
		}
		
		public function enableSound():void
		{
			soundFlag = true;
			this.filters = null;
			SoundMixer.soundTransform = new SoundTransform(1);
		} 
		
		public function disableSound():void
		{
			soundFlag = false;	
			this.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
			SoundMixer.soundTransform = new SoundTransform(0);
		}
		
		public function switchStatus():void
		{
			if (soundFlag)
			{
				disableSound();
			}
			else
			{
				enableSound();
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			if (tip == null)
			{
				tip = new BasicMessage(52, 25, "声音");
				tip.x = this.x + (30 - tip.width) / 2;
				tip.y = this.y + 40;
				this.parent.addChild(tip);
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			if (null !=  tip && null != tip.parent)
        	{
        		tip.parent.removeChild(tip);
        		tip = null;
        	}
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			switchStatus();
		}
	}
}